﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectShare
{
    public class Common
    {
        public static string GetOldYearStr(string CurrentYearStr)
        {
            if(!string.IsNullOrEmpty(CurrentYearStr))
            {
                int CurrentYear = Convert.ToInt16(System.Text.RegularExpressions.Regex.Replace(CurrentYearStr, @"[^0-9]+", ""));
                int OldYear = CurrentYear - 1;
                return OldYear + "年";
            }
            else
            {
                return "";
            }
         }
        /// <summary>
        /// 获取增减比
        /// </summary>
        /// <param name="OldNum"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static string GetDivision(decimal? OldNum, decimal ? Num)
        {
            string ResultNum = "";
            if(OldNum!=null&&Num!=null)
            {
                if (OldNum != decimal.Zero)
                {
                    ResultNum = Math.Round(GetDouble(((Num - OldNum) / OldNum)*100),1).ToString();
                }
            }
            return ResultNum;
        }
        /// <summary>
        /// A/B
        /// </summary>
        /// <param name="NumA"></param>
        /// <param name="NumB"></param>
        /// <returns></returns>
        public static string GetCF(decimal? NumA,decimal? NumB)
        {
            string Result = "";
            if(NumA!=null&&NumB!=null)
            {
                if(NumB!=decimal.Zero)
                {
                    Result = (Math.Round(Convert.ToDouble(NumA / NumB), 1)).ToString();
                }
            }
            return Result;
        }
        public static Double GetDouble(decimal ? Num)
        {
            if (Num != null)
            {
                return Convert.ToDouble(Num);
            }
            else
            {
                return 0;
            }
        }
        public static decimal GetDecimal(decimal? Num)
        {
            if (Num != null)
            {
                return Convert.ToDecimal(Num);
            }
            else
            {
                return decimal.Zero;
            }
        }
        /// <summary>
        /// 获取一位小数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetADecimal(decimal? value)
        {
            if(value!=null)
            {
                return Math.Round(GetDouble(value), 1).ToString();
            }
            else
            {
                return "0.0";
            }
        }
        /// <summary>
        /// 获取两位小数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetBDecimal(decimal? value)
        {
            if (value != null)
            {
                return Math.Round(GetDouble(value), 2).ToString();
            }
            else
            {
                return "0.0";
            }
        }
        public static bool IsNumeric(string str) //接收一个string类型的参数,保存到str里
        {
            if (str == null || str.Length == 0)    //验证这个参数是否为空
                return false;                           //是，就返回False
            ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
            byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里

            foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是，就返回False
                }
            }
            return true;                                        //是，就返回True
        }
    }
}
